package com.jason.www.dao;

import com.jason.www.domain.Permissions;
import com.jason.www.domain.Roles;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;

import java.util.Set;

public interface RolesMapper {
    @Delete({
        "delete from roles",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into roles (id, name)",
        "values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR})"
    })
    int insert(Roles record);

    @InsertProvider(type=RolesSqlProvider.class, method="insertSelective")
    int insertSelective(Roles record);

    @Select({
        "select",
        "id, name",
        "from roles",
        "where id = #{id,jdbcType=BIGINT}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR)
    })
    Roles selectByPrimaryKey(Long id);

    @UpdateProvider(type=RolesSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Roles record);

    @Update({
        "update roles",
        "set name = #{name,jdbcType=VARCHAR}",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(Roles record);

    @Select({
            "SELECT p.* FROM roles r,roles_permissions rp ,permissions p " +
                    "WHERE r.`id`=rp.`roles_id` " +
                    "AND rp.`permissions_id`=p.`id` " +
                    "AND r.`id` IN (#{rolesIdSet})"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="theurl", property="theurl", jdbcType=JdbcType.VARCHAR),
            @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR)
    })
    Set<Permissions> getPermissionsByRolesIds(Set<Long> rolesIdSet);
}